﻿/* ***********************************************
 * 创建者:  涂云横
 * 创建时间  :  11/2/2010 7:42:45 PM
 * 功能描述: 
 * 1、文件管理
 * 2、写文件和读文件
 * modification history
 * 修改者：涂云横
 * 修改时间：11/2/2010 7:42:45 PM
 * 修改描述：
 * 1、
 * 2、
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using com.dz.common;

namespace com.dz.components.background
{
    /* ***********************************************
   * 功能描述: 
   * 1、文件管理类
   * 2、写文件内容、读文件内容、主要用于章节内容信息存放
   * ***********************************************/
    public class FileManager
    {



        /// <summary>
        /// 读取书的章节信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <returns></returns>
        public string GetBookContent(long i_book_id, long i_chapter_id)
        {
            string t_path = AppConfig.BookChapterRoot;
            string t_file_name = Path.Combine(t_path, i_book_id.ToString());
            t_file_name = Path.Combine(t_file_name, string.Format("{0}.txt", i_chapter_id));
            if (!File.Exists(t_file_name))
            {

                LogHelper.Error("读取文件" + t_file_name + "失败,文件不存在!");
                return string.Empty;
            }

            //逐行读取文本内容
            string t_text;

            try
            {
                using (StreamReader t_sm_read = new StreamReader(t_file_name, System.Text.Encoding.Default))
                {
                    t_text = t_sm_read.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("读取文件出现异常" + ex.ToString());
                return string.Empty;
            }
            return t_text;
        }

        /// <summary>
        /// 写书的章节文本
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <param name="i_text"></param>
        /// <returns></returns>
        public int WriteBookContent(long i_book_id, long i_chapter_id, string i_text)
        {
            string t_path = AppConfig.BookChapterRoot;
            t_path = Path.Combine(t_path, i_book_id.ToString());
            if (!Directory.Exists(t_path))
            {
                Directory.CreateDirectory(t_path);
            }
            string t_file_name = Path.Combine(t_path, string.Format("{0}.txt", i_chapter_id));
            try
            {
                FileInfo t_check_file = CreatFile(t_file_name);
                StreamWriter writer = t_check_file.AppendText();
                writer.WriteLine(i_text);
                writer.Flush();
                writer.Close();
                return Consts.Successed;
            }
            catch (Exception ex)
            {
                LogHelper.Error("写文件信息失败" + ex.ToString());
                return Consts.Failed;
            }
        }

        /// <summary>
        /// 检查章节内容是否已存在
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <returns></returns>
        public bool CheckChaperExist(long i_book_id, long i_chapter_id)
        {
            try
            {
                string t_path = AppConfig.BookChapterRoot;
                t_path = Path.Combine(t_path, i_book_id.ToString());
                if (!Directory.Exists(t_path))
                {
                    Directory.CreateDirectory(t_path);
                }
                string t_file_name = Path.Combine(t_path, string.Format("{0}.txt", i_chapter_id));

                return File.Exists(t_file_name);
            }
            catch (Exception ex)
            {
                LogHelper.Error("检查章节内容是否存在失败" + ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 创建/读取检查文件
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        private FileInfo CreatFile(string i_fileName)
        {
            FileInfo t_checkedFile = null;
            try
            {
                t_checkedFile = new FileInfo(i_fileName);
            }
            catch (Exception ex)
            {
                LogHelper.Error("创建读取检查文件失败" + ex.ToString());
            }

            return t_checkedFile;
        }

    }
}
